import '../filter/gt_logger_base_filter.dart';
import '../gt_logger_color.dart';
import '../gt_logger_level.dart';
import '../gt_logger_model.dart';

/// Author: Shper
/// Created: 2024/2/01
abstract class GTLoggerBaseDestination {

  /// 单独过滤器列表
  final List<GTLoggerBaseFilter> filters = [];

  /// 添加过滤器
  void addFilter(GTLoggerBaseFilter filter) {
    filters.add(filter);
  }

  /// 定义日志级别的色值
  final Map<GTLoggerLevel, GTLoggerColor> _levelColors = {
    GTLoggerLevel.verbose: const GTLoggerColor.none(),
    GTLoggerLevel.debug: const GTLoggerColor.green(),
    GTLoggerLevel.info: const GTLoggerColor.blue(),
    GTLoggerLevel.warning: const GTLoggerColor.yellow(),
    GTLoggerLevel.error: const GTLoggerColor.red(),
  };

  /// 获取日志色值
  GTLoggerColor getLevelColor(GTLoggerLevel level) {
    GTLoggerColor? color = _levelColors[level];
    return color ?? const GTLoggerColor.none();
  }

  /// 接收日志
  Future<void> receiveLog(GTLoggerModel logModel) async {
    // 是否忽略日志
    if (ignoreLog(logModel)) {
      return;
    }
    // 先通过过滤器处理日志
    for (GTLoggerBaseFilter filter in filters) {
      logModel = filter.handleFilter(logModel);
    }
    // 处理日志
    handleLog(logModel);
  }

  /// 是否要忽略日志
  bool ignoreLog(GTLoggerModel logModel);

  /// 处理日志
  void handleLog(GTLoggerModel logModel);

}